package com.tboss.template.common.dwr;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;

import com.tboss.template.system.dao.CodeDao;
import com.tboss.template.system.dao.CodeTypeDao;
import com.tboss.template.system.dao.FunctionDao;
import com.tboss.template.system.dao.GroupDao;
import com.tboss.template.system.dao.RoleDao;
import com.tboss.template.system.dao.UserInfoDao;
import com.tboss.template.system.dto.CodeDto;
import com.tboss.template.system.dto.FunctionDto;

@RemoteProxy(name="checkDuplication")
public class CheckDuplication {

	@Autowired private CodeTypeDao cdTpDao;
	@Autowired private CodeDao cdDao;
	@Autowired private FunctionDao fnDao;
	@Autowired private GroupDao grpDao;
	@Autowired private UserInfoDao usrInfoDao;
	@Autowired private RoleDao roleDao;
	
	private StringBuffer getConvertToJson(Boolean rtnRst, String rtnFild, String rtnMsg, String actionType){
		StringBuffer rtnValue = new StringBuffer();
		
		try {
			Map<String, Object> rtnJson = new HashMap<String, Object>();
			rtnJson.put("result", rtnRst);
			rtnJson.put("chkField", rtnFild);			
			rtnJson.put("msg", rtnMsg);
			rtnJson.put("actionType", actionType);
			rtnValue.append(new ObjectMapper().writeValueAsString(rtnJson));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		} catch (Exception e) { e.printStackTrace();
		}
		
		return rtnValue;
	}
	
	@RemoteMethod
	public String existCdTpId(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");

		Integer idCnt = (actionType != null && actionType.toLowerCase().equals("new")) 
				? this.cdTpDao.existCodeTypeId(chkFields.get("cdTpId")) : 0;				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "cdTpId";
			rtnMsg = "이미 등록된 아이디 입니다.";
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}

	@RemoteMethod
	public String existCdFields(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");
		
		Integer idCnt = (actionType != null && actionType.toLowerCase().equals("new")) 
				? this.cdDao.existCodeId(chkFields.get("cdId")) : 0;				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "cdId";
			rtnMsg = "이미 등록된 코드 입니다.";
		
		} else{
			CodeDto dto = new CodeDto();
			dto.setCdId(chkFields.get("cdId"));
			dto.setCdAlias(chkFields.get("cdAlias"));
			dto.setCdTpId(chkFields.get("cdTpId"));
			
			Integer aliasCnt = this.cdDao.existCodeAlias(dto);			
			if(aliasCnt > 0){ 
				rtnRst = true;
				rtnFild = "cdAlias";
				rtnMsg = "이미 사용 중인 코드 명칭입니다.";
			}
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}
	
	@RemoteMethod
	public String existFnId(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");
		String cdId = chkFields.get("cdId");
		String fnId = chkFields.get("fnId");
		String fnAlias = chkFields.get("fnAlias");

		Integer idCnt = (actionType.toLowerCase().equals("new")) 
				? this.fnDao.existFnId(fnId) : ((fnId.equals(cdId)) ? 0 : this.fnDao.existFnId(fnId));				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "fnId";
			rtnMsg = "이미 등록된 코드 입니다.";
		}else{
			FunctionDto dto = new FunctionDto();
			dto.setFnId((actionType.toLowerCase().equals("new")) ? fnId : cdId);
			dto.setFnAlias(fnAlias);
			
			Integer aliasCnt = this.fnDao.existFnAlias(dto);			
			if(aliasCnt > 0){ 
				rtnRst = true;
				rtnFild = "fnAlias";
				rtnMsg = "이미 사용 중인 기능 명칭입니다.";
			}
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}
	
	@RemoteMethod
	public String existGrpId(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");

		Integer idCnt = (actionType != null && actionType.toLowerCase().equals("new")) 
				? this.grpDao.existGrpId(chkFields.get("grpId")) : 0;				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "grpId";
			rtnMsg = "이미 등록된 아이디 입니다.";
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}
	
	@RemoteMethod
	public String existUsrInfoId(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");

		Integer idCnt = (actionType != null && actionType.toLowerCase().equals("new")) 
				? this.usrInfoDao.existUsrInfoId(chkFields.get("usrId")) : 0;				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "usrId";
			rtnMsg = "이미 등록된 아이디 입니다.";
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}
	
	@RemoteMethod
	public String existRoleId(HashMap<String, String> chkFields){
		Boolean rtnRst = false;
		String rtnFild = "";
		String rtnMsg = "";
		String actionType = chkFields.get("actionType");

		Integer idCnt = (actionType != null && actionType.toLowerCase().equals("new")) 
				? this.roleDao.existRoleId(chkFields.get("roleId")) : 0;				
		if(idCnt > 0){ 
			rtnRst = true;
			rtnFild = "roleId";
			rtnMsg = "이미 등록된 아이디 입니다.";
		}
		
		return this.getConvertToJson(rtnRst, rtnFild, rtnMsg, chkFields.get("actionType")).toString();
	}
	
}
